//
//  YJShareView.m
//  Artv
//
//  Created by SC on 16/7/26.
//  Copyright © 2016年 SC. All rights reserved.
//

#import "YJShareView.h"
#import "WXApi.h"
#import <UMSocialCore/UMSocialCore.h>   //友盟分享头文件

@interface YJShareView ()

@end

@implementation YJShareView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, WIDTH, WIDTH*50/375);
        self.backgroundColor = HEXCOLOR(kColorGray7, 1);
        [self setUI];
    }
    return self;
}

- (void)setUI
{
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.frame = CGRectMake(10, (WIDTH*50/375-20)/2, 45, 20);
    leftLabel.text = @"分享到:";
    leftLabel.font = [UIFont systemFontOfSize:13];
    leftLabel.textColor = HEXCOLOR(kColorBlue2, 1);
    [self addSubview:leftLabel];
    
    _vipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _vipBtn.frame = CGRectMake(WIDTH-100, 0, 100, WIDTH*50/375);
    [_vipBtn setTitle:@"升级VIP" forState:UIControlStateNormal];
    [_vipBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _vipBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_vipBtn addTarget:self action:@selector(vipAction) forControlEvents:UIControlEventTouchUpInside];
    _vipBtn.backgroundColor = HEXCOLOR(kColorBlue1, 1);
    [self addSubview:_vipBtn];
    
    [self setShareBtn];
}

- (void)setShareBtn
{
    UIView *bgShareView = [[UIView alloc] init];
    bgShareView.frame = CGRectMake(60, 0, WIDTH-60-110,  WIDTH*50/375);
    bgShareView.backgroundColor = [UIColor clearColor];
    [self addSubview:bgShareView];
    
    BOOL installtWX = [WXApi isWXAppInstalled];//微信
    BOOL installQQ = [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];//QQ
    
    CGFloat shareBtnW=(WIDTH-60-110-65)/4;
    NSArray *colorArray;
    IconFont *iconFont = [IconFont shareInstance];
    if (installtWX && installQQ) {//都安装
        _shareArr = @[iconFont.iconpengyouquan,
                      iconFont.iconqq,
                      iconFont.iconweixin,
                      iconFont.iconweibo];
        colorArray = @[HEXCOLOR(kColorGreen2, 1),HEXCOLOR(kColorBlue3, 1),HEXCOLOR(kColorGreen1, 1),HEXCOLOR(kColorOrange1, 1)];
    }
    else if (installtWX && !installQQ) {//微信安装，QQ没安装
        _shareArr = @[iconFont.iconpengyouquan,
                      iconFont.iconweixin,
                      iconFont.iconweibo];
        colorArray = @[HEXCOLOR(kColorGreen2, 1),HEXCOLOR(kColorGreen1, 1),HEXCOLOR(kColorOrange1, 1)];
    }
    else if (!installtWX && installQQ) {//微信没安装，QQ安装
        _shareArr = @[iconFont.iconqq,
                      iconFont.iconweibo];
        colorArray = @[HEXCOLOR(kColorBlue3, 1),HEXCOLOR(kColorOrange1, 1)];
    }
    else
    {//都没安装
        _shareArr = @[iconFont.iconweibo];
        colorArray = @[HEXCOLOR(kColorOrange1, 1)];
    }
    
    for (int i=0; i<_shareArr.count; i++) {
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        shareBtn.frame = CGRectMake(10+i*(shareBtnW+15), (WIDTH*50/375-shareBtnW)/2, shareBtnW, shareBtnW);
        
        if (iPhone5) {
            shareBtn.titleLabel.font = [UIFont fontWithName:@"iconfont" size:20];
        }else{
            shareBtn.titleLabel.font = [UIFont fontWithName:@"iconfont" size:30];
        }
        
        [shareBtn setTitle:_shareArr[i] forState:UIControlStateNormal];
        [shareBtn setTitleColor:colorArray[i] forState:UIControlStateNormal];
        shareBtn.tag = 100+i;
        [shareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
        [bgShareView addSubview:shareBtn];
    }
}

//分享方法
- (void)shareAction:(UIButton *)shareBtn
{
    if ([_delegate respondsToSelector:@selector(shareDelegateMethod:count:)]) {
        [_delegate shareDelegateMethod:shareBtn.tag count:_shareArr.count];
    }
}


//升级VIP
- (void)vipAction
{
    if ([_delegate respondsToSelector:@selector(vipDelegateMethod)]) {
        [_delegate vipDelegateMethod];
    }
}

@end
